package com.pocketsweet.ui.uilib.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pocketsweet.R;
import com.pocketsweet.model.MLGift;
import com.pocketsweet.model.MLGiftRecord;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.model.MLUserLike;
import com.pocketsweet.service.UserService;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

import java.util.List;

/**
 * @author Kam
 */
public class GiftRecordAdapter extends BaseListAdapter<MLGiftRecord> {
    private Context context;

    public GiftRecordAdapter(Context ctx) {
        super(ctx);
        this.context = ctx;
    }

    public GiftRecordAdapter(Context ctx, List<MLGiftRecord> datas) {
        super(ctx, datas);
        this.context = ctx;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_gift_record, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        viewHolder.refreshView(datas.get(position), position);
        return convertView;
    }

    public class ViewHolder {
        ImageView imgAvatar;
        ImageView imgGift;
        TextView tvuserName;
        TextView tvTimeDiff;
        TextView tvGiftName;
        TextView tvGiftNum;
        TextView tvGiftInfo;
        MLGiftRecord item;
        MLUser mlUser;
        MLGift mlGift;
        int likeCount = 0;
        public View parent;

        public ViewHolder(View parent) {
            this.parent = parent;
            initView(parent);
        }

        public void initView(View parent) {
            imgAvatar = (ImageView) parent.findViewById(R.id.imgAvatar);
            imgGift = (ImageView) parent.findViewById(R.id.imgGift);
            tvuserName = (TextView) parent.findViewById(R.id.tvuserName);
            tvTimeDiff = (TextView) parent.findViewById(R.id.tvTimeDiff);
            tvGiftName = (TextView) parent.findViewById(R.id.tvGiftName);
            tvGiftInfo = (TextView) parent.findViewById(R.id.tvGiftInfo);
            tvGiftNum = (TextView) parent.findViewById(R.id.tvGiftNum);
        }

        public void refreshView(MLGiftRecord item, int i) {
            this.item = item;
            // 设置图片
            mlUser = item.getUser();
            mlGift = item.getGift();
            if (mlUser != null) {
                String avatar = mlUser.getAvatarUrl();
                if (avatar != null && !avatar.equals("")) {
                    UserService.displaySquare(avatar, imgAvatar);
                } else {
                    imgAvatar.setImageResource(R.drawable.default_user_avatar);
                }
                if (!StringUtils.isEmpty((mlUser.getNickname()))) {
                    tvuserName.setText(mlUser.getNickname());
                }
                tvGiftName.setText(mlGift.getName());
                tvGiftInfo.setText(mlGift.getInfo());
                String giftImg = mlGift.getImgUrl();
                if (giftImg != null && !giftImg.equals("")) {
                    UserService.displaySquare(giftImg, imgGift);
                } else {
                    imgGift.setImageResource(R.drawable.default_user_avatar);
                }
                tvTimeDiff.setText(ToolKits.showCommentTime(item.getCreatedAt()));// 几分钟之前

            }
        }

    }

}
